package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.dto.SmsSpikeProductDto;
import com.mall.entity.PmsProduct;
import com.mall.entity.SmsSpikeSession;
import com.mall.common.api.Code;
import com.mall.common.api.Result;
import com.mall.mapper.SmsSpikeCommodityMapper;
import com.mall.service.SmsSpikeCommodityService;
import com.mall.service.SmsSpikeSessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(tags = "SmsCouponCategoryController", description = "秒杀活动时间段管理")
@RestController
@RequestMapping("/spikeSessions")
public class SmsSpikeSessionController {

    @Resource
    private SmsSpikeSessionService smsSpikeSessionService;


    @ApiOperation(value = "新增秒杀活动时间段")
    @PostMapping
    public Result save(@RequestBody SmsSpikeSession smsSpikeSession) {
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String current = formatter.format(date);
        smsSpikeSession.setCreateTime(current);
        boolean flag = smsSpikeSessionService.save(smsSpikeSession);
        return new Result(flag? Code.SAVE_OK: Code.SAVE_ERR,flag);
    }

    @ApiOperation(value = "根据id删除秒杀活动时间段")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = smsSpikeSessionService.deleteById(id);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @ApiOperation(value = "根据id查询秒杀活动时间段")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        SmsSpikeSession smsSpikeSession = new SmsSpikeSession();
        smsSpikeSession = smsSpikeSessionService.getById(id);
        Integer code = smsSpikeSession !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = smsSpikeSession !=null ? "successful":"failed,please try again";
        return new Result(smsSpikeSession,code,msg);
    }

    @ApiOperation(value = "查询全部秒杀活动时间段")
    @GetMapping
    public Result getAll(){
        List<SmsSpikeSession> list = smsSpikeSessionService.getAll();
        Integer code = list!=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list!=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @ApiOperation(value = "根据id修改时间段启用状态")
    @PutMapping("/{id}")
    public Result updateById(@PathVariable Integer id) {
        Integer key = smsSpikeSessionService.getById(id).getStatus();
        SmsSpikeSession smsSpikeSession = smsSpikeSessionService.getById(id);
        if(key==1)
        {
            smsSpikeSession.setStatus(0);
        }else
        {
            smsSpikeSession.setStatus(1);
        }
        boolean flag = smsSpikeSessionService.save(smsSpikeSession);
        return new Result(flag?Code.UPDATE_OK: Code.UPDATE_ERR,flag);
    }

    @PostMapping ("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        boolean flag = smsSpikeSessionService.removeByIds(ids);
        return new Result(flag?Code.DELETE_OK: Code.DELETE_ERR,flag);
    }

    @GetMapping("/page")
    public IPage<SmsSpikeSession> getPage(@RequestParam Integer pageNum,
                                          @RequestParam Integer pageSize,
                                          @RequestParam String title) {
        IPage<SmsSpikeSession> page = new Page<>(pageNum,pageSize);
        QueryWrapper<SmsSpikeSession> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        return smsSpikeSessionService.page(page,queryWrapper);
    }

    @ApiOperation(value = "查询当前的秒杀商品")
    @GetMapping("/nowProduct")
    public Result getNowProduct(){
        List<SmsSpikeProductDto> list = smsSpikeSessionService.findNowProducts();
        Integer code = list !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = list !=null ? "successful":"failed,please try again";
        return new Result(list,code,msg);
    }

    @ApiOperation(value = "获取当前时间段截止时间")
    @GetMapping("/getEndTime")
    public Result getEndTime(){
        String endTime = smsSpikeSessionService.getEndTime();
        Integer code = endTime !=null ? Code.GET_OK:Code.GET_ERR;
        String msg = endTime !=null ? "successful":"failed,please try again";
        return new Result(endTime,code,msg);
    }
}